package cn.wolfcode.web.controller;

import cn.wolfcode.qo.CustomerReportQuery;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        PageInfo pageInfo = customerReportService.selectForList(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "customerReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model,@ModelAttribute("qo") CustomerReportQuery qo){
        //查询数据库的分组数据
        List<HashMap> list = customerReportService.selectAll(qo);

        //先准备两个集合,分别存储x和y的数据
        List x = new ArrayList();
        List y = new ArrayList();
        //遍历sql查出来的list,获取每一个map
        for (HashMap map : list){
            //从map中获取对应的数据,分别放入x和y的集合
            x.add(map.get("groupType"));
            y.add(map.get("number"));
        }

        //共享到页面
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        model.addAttribute("x", JSON.toJSONString(x));
        model.addAttribute("y", JSON.toJSONString(y));
        return "customerReport/listBar";
    }

    /*public static void main(String[] args) {
        List x = new ArrayList();
        x.add("admin");
        x.add("admin");
        x.add("admin");
        x.add("admin");
        System.out.println(JSON.toJSONString(x));
    }*/

    @RequestMapping("/listPie")
    public String listPie(Model model,@ModelAttribute("qo") CustomerReportQuery qo){
        //查询数据库的分组数据
        List<HashMap> list = customerReportService.selectAll(qo);

        //先准备两个集合,分别存储x和y的数据
        List legend = new ArrayList();
        List series = new ArrayList();
        //遍历sql查出来的list,获取每一个map
        for (HashMap map : list){

            legend.add(map.get("groupType"));
            HashMap temp = new HashMap();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("number"));
            series.add(temp);
        }

        //共享到页面
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        model.addAttribute("legend", JSON.toJSONString(legend));
        model.addAttribute("series", JSON.toJSONString(series));
        return "customerReport/listPie";
    }

}
